#include "fileOperationsHeader.h"
char* exportFile (char *vfs_file_path,char *hard_disk_path)
{
	if(mh==NULL)
		return ERR_VFS_EXPORTFILE_07;
    FILE *fp=fopen(mh->fileSystemLabel,"r+b");
    int currentFileDescriptors=mh->countOfFileDescriptors;
	int maxFileDescriptors=mh->maxDescriptors;
	int noOfBlocks=maxFileDescriptors/2;
	int i=strlen(vfs_file_path)-1;
	for(;vfs_file_path[i]!='/';i--);
	char *srcParent=(char*)malloc(sizeof(char)*(i+1));
	int j=0;
	for(;j<i;j++)
		srcParent[j]=vfs_file_path[j];
	srcParent[j]='\0';
	char *src=(char*)malloc(sizeof(char)*(strlen(vfs_file_path)-1-i+1));
	j=0;
	i++;
	for(;vfs_file_path[i]!='\0';j++)
	{
		src[j]=vfs_file_path[i];
		i++;
	}
	src[j]='\0';
	if(strcmp(srcParent,"")==0 || strcmp(srcParent," ")==0)
		srcParent="/";
	descriptors *d ;
	d=(descriptors*)searchBST(vfs_file_path);
	int iterDescriptors=0;
	if(d==NULL)
	{
		 while(iterDescriptors<maxFileDescriptors)
         {
             if(strcmp(h->desc[iterDescriptors].fileName,src)==0 && strcmp(h->desc[iterDescriptors].location,srcParent)==0 && h->desc[iterDescriptors].type=='d')
                 return ERR_VFS_EXPORTFILE_06;
             iterDescriptors++;
         }
         //fclose(fp);
		 return ERR_VFS_EXPORTFILE_01;
	}
	int s=d->size;
	int address=(int)arr[d->blockNumber];
	fseek(fp,(int)arr[d->blockNumber],SEEK_SET);
	char *str=(char*)malloc(sizeof(char)*s);
	int c = fread(str,s,1,fp);
	FILE *fileHD = fopen(hard_disk_path,"w");
	if(fileHD==NULL)
		return ERR_VFS_EXPORTFILE_02;
	fwrite(str,s,1,fileHD);
    fclose(fileHD);
    //fclose(fp);
    int bstRet=insertBSTWhileMount(maxFileDescriptors,h->desc);
	if(bstRet!=0)
		return "FAILURE <while creating BST>";
	int nAryRet=createnAryTree(maxFileDescriptors,h->desc);
	if(nAryRet!=0)
		return "FAILURE <while creating nAryTree>";
	//traversenAryTree(nAryRoot);
	int hashTableRet=hashTableMethod(h->desc,maxFileDescriptors);
	if(hashTableRet!=0)
		return "FAILURE <while creating hashTable>";
    return "SUCCESS";
}

